#! /bin/bash
# rcrsn51 2013-03-14

export TEXTDOMAIN=legacy_grub_2013
export OUTPUT_CHARSET=UTF-8

INSTALL () {

# $1=stage1  $2=partition  $3=stage2  $4=windows  $5=Ubuntu  $6:true=frugal,false=full 

TIMEOUT=2
[ -n "$4" -o -n "$5" ] && TIMEOUT=10

mount /dev/$2 /mnt/data
if [ $? -gt 0 ]; then
  Xdialog --title " " --msgbox "$(gettext 'Cannot mount') $2" 0 0
  exit
fi

GRUBPATH="/mnt/data/boot/grub"
mkdir -p $GRUBPATH
if [ $? -gt 0 ]; then
  Xdialog --title " " --msgbox "$(gettext 'Cannot create') $GRUBPATH" 0 0
  exit
fi

cp $(find /usr/lib/grub -name stage1) $GRUBPATH
cp $(find /usr/lib/grub -name stage2) $GRUBPATH

grub --batch --no-floppy << EOF
root $3
setup $1
quit
EOF

[ -f $GRUBPATH/menu.lst ] && mv $GRUBPATH/menu.lst $GRUBPATH/menu.lst.000

cat > $GRUBPATH/menu.lst << EOF
timeout $TIMEOUT
default 0
#
EOF

if [ "$6" = "true" ]; then

cat >> $GRUBPATH/menu.lst << EOF
title Puppy Linux Frugal on $2
root $3
kernel /vmlinuz pmedia=atahd pdev1=$2 pfix=fsck
initrd /initrd.gz
EOF

else

cat >> $GRUBPATH/menu.lst << EOF
title Puppy Linux Full on $2
root $3
kernel /boot/vmlinuz root=/dev/$2 ro vga=normal
EOF

fi

if [ -n "$4" ]; then
cat >> $GRUBPATH/menu.lst << EOF
#
title Windows on $4
root $4
chainloader +1
EOF
fi

if [ -n "$5" ]; then
cat >> $GRUBPATH/menu.lst << EOF
#
title Ubuntu on $5
root $5
kernel /boot/grub/core.img
EOF
fi

sync
umount /mnt/data

}

############### end of functions #############

export Grub_config='
<window title="Legacy GRUB Config 2013" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed "startup_os.svg" 60 "$(gettext 'Legacy GRUB has two parts. Stage1 is the bootloader code. It can be installed in the MBR of a hard drive or in a partition boot sector. Stage2 is the set of support files, like menu.lst. It is installed in the folder /boot/grub of a partition. Legacy GRUB works with ext or FAT filesystems, but not NTFS.')"`"' 
  <notebook labels="'$(gettext 'Quick mode')'|'$(gettext 'Expert mode')'" space-expand="true" space-fill="true">
    <vbox space-expand="true" space-fill="true">
      <frame '$(gettext 'Quick mode')'>
        <hbox space-expand="true" space-fill="true">
          <text xalign="0" yalign="0" space-expand="true" space-fill="true">
            <label>"'$(gettext 'Install Stage1 in the MBR of Drive sda
and Stage2 in sda1')'"</label>
          </text>
          <vbox space-expand="false" space-fill="false">
            <text xalign="0"><label>'$(gettext 'Puppy Setup')'</label></text>
            <radiobutton>
              <variable>QFRUG</variable>
              <label>'$(gettext 'Frugal')'</label>
            </radiobutton>
            <radiobutton> 
              <variable>QFULL</variable>
              <label>'$(gettext 'Full')'</label>
            </radiobutton>
            <text space-expand="true" space-fill="true"><label>""</label></text>
            <button image-position="2">
              <label>" '$(gettext 'Quick Install')' "</label>
              '"`/usr/lib/gtkdialog/xml_button-icon install.svg big`"'
            </button>
          </vbox>
        </hbox>
      </frame>
    </vbox>
    <vbox space-expand="true" space-fill="true">
      <frame '$(gettext 'Expert mode')'>
        <hbox space-expand="true" space-fill="true">
          <vbox space-expand="false" space-fill="false">
            <hbox>
              <entry width-request="70" space-expand="false" space-fill="false">
                <variable>STAGE1</variable>
                <default>(hd0)</default>
              </entry>
              <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Target for Stage1')'</label></text>
            </hbox>
            <hbox>
              <entry width-request="70" space-expand="false" space-fill="false">
                <variable>PART</variable>
                <default>sda1</default>
              </entry>
              <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Partition for Stage2')'</label></text>
            </hbox>
            <hbox>
              <entry width-request="70" space-expand="false" space-fill="false">
                <variable>STAGE2</variable>
                <default>(hd0,0)</default>
              </entry>
              <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Target for Stage2')'</label></text>
            </hbox>
            <hbox>
              <entry width-request="70" space-expand="false" space-fill="false">
                <variable>WIN</variable>
                <default>(hd0,0)</default>
              </entry>
              <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Optional Windows entry')'</label></text>
            </hbox>
            <hbox>
              <entry width-request="70" space-expand="false" space-fill="false">
                <variable>UBUNTU</variable>
                <default>(hd0,0)</default>
              </entry>
              <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Optional Ubuntu entry')'</label></text>
            </hbox>
          </vbox>
          <text space-expand="true" space-fill="true"><label>""</label></text>
          <vbox space-expand="false" space-fill="false">
            <text xalign="0"><label>'$(gettext 'Puppy Setup')'</label></text>
            <radiobutton>
              <variable>EFRUG</variable>
              <label>'$(gettext 'Frugal')'</label>
            </radiobutton>
            <radiobutton> 
              <variable>EFULL</variable>
              <label>'$(gettext 'Full')'</label>
            </radiobutton>
            <text space-expand="true" space-fill="true"><label>""</label></text>
            <button image-position="2">
              <label>" '$(gettext 'Expert Install')' "</label>
              '"`/usr/lib/gtkdialog/xml_button-icon install_options.svg big`"'
            </button>
          </vbox>
        </hbox>
      </frame>
    </vbox>
  </notebook>
  <hbox space-expand="false" space-fill="false"> 
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon help_online.svg`"'
      <label>'$(gettext 'Help')'</label>
    </button>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
    </button>
  </hbox>
</vbox>
</window>'

 . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog -p Grub_config); do
  eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "$(gettext 'Cancel')" -o "$EXIT" = "abort" ] && exit

[ "$EXIT" = "$(gettext 'Help')" ] && exec defaultbrowser http://murga-linux.com/puppy/viewtopic.php?p=692147#692147

Xdialog --title " " --yesno "$(gettext 'Do you really want to install GRUB?')" 0 0
[ $? -gt 0 ] && exit

if [ "$EXIT" = "$(gettext 'Quick Install')" ]; then			#quick install
  INSTALL "(hd0)" "sda1" "(hd0,0)" "" "" "$QFRUG" 			#true=frugal, false=full
else									#expert install
  INSTALL "$STAGE1" "$PART" "$STAGE2" "$WIN" "$UBUNTU" "$EFRUG" 	#true=frugal, false=full
fi

/usr/lib/gtkdialog/box_ok "Legacy GRUB Config 2013" complete "<b>$(gettext 'Done')</b>"
